using System;
using System.Diagnostics;
using System.Collections.Generic;
using org.tmatesoft.svn.core;
using org.tmatesoft.svn.core.wc;

namespace Subsync.Core.Subversion
{
	/// <summary>
	/// Author: Zhang Andy
	/// </summary>
    class SVNLock : CommandBase
    {
        // Initialise variables with default settings.
        private java.io.File[] file_path; //url of file to be locked
        private bool stealLock = true; //other users may take over the lock
        private string lockMessage; //compulsory field

        // Minimal constructor1.
        public SVNLock(string strpath, string message)
        {
            Debug.WriteLine("SVN Lock minimal Constructor with defaults");
            List<string> path = new List<string>();
            path.Add(strpath);
            this.lockMessage = message;
            this.file_path = new java.io.File[path.Count];
            for (int i = 0; i < path.Count; i++)
                file_path[i] = new java.io.File(path[i]);
        }

        // Minimal constructor2.
        public SVNLock(List<string> path, string message)
        {
            Debug.WriteLine("SVN Lock minimal Constructor with defaults");
            this.lockMessage = message;
            this.file_path = new java.io.File[path.Count];
            for (int i = 0; i < path.Count; i++)
                file_path[i] = new java.io.File(path[i]);
        }

        // Full constructor
        public SVNLock(List<string> path, bool stealLock, string message)
        {
            Debug.WriteLine("SVN Lock full Constructor with defaults");
            this.lockMessage = message;
            this.stealLock = stealLock;
            this.file_path = new java.io.File[path.Count];
            for (int i = 0; i < path.Count; i++)
                file_path[i] = new java.io.File(path[i]);
        }

        public override void Execute()
        {
            Debug.WriteLine("SVN Lock Execution");

            //library setup
            Tools.SetupLibrary();

            //load up the clients
            SVNClientManager cm = SVNClientManager.newInstance();
            SVNWCClient wc = cm.getWCClient();

            try
            {
                wc.doLock(this.file_path, this.stealLock, this.lockMessage);
            }
            catch (SVNException e)
            {
                //we convert this exception to a non SVNKit exception.
                //we want to minimize interfacing with SVNkit outside of this namespace (abstraction)
                throw new SVNExecuteException(e.getMessage());
            }
        }
    }
}
